﻿using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mini.Common
{
    public class ProgramHelper
    {
        public static string ShowDialog(string text, string caption)
        {
            var prompt = new Form
            {
                Width = 500,
                Height = 150,
                Text = caption
            };
            var textLabel = new Label()
            {
                Left = 50,
                Top = 20,
                Text = text
            };
            var textBox = new TextBox()
            {
                Left = 50,
                Top = 50,
                Width = 400
            };
            var confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70 };
            confirmation.Click += (sender, e) => prompt.Close();
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(textBox);
            prompt.ShowDialog();
            if (string.IsNullOrEmpty(textBox.Text))
            {
                ShowDialog(text, caption);
            }
            return textBox.Text;
        }

        public static void ClearInputForm(Form form)
        {
            foreach (Control control in form.Controls)
            {
                if (control is TextBox)
                {
                    control.Text = string.Empty;
                }
            }
        }

        public static void EnableButton(Form form, bool enable)
        {
            foreach (Control control in form.Controls)
            {
                if (control is Button)
                {
                    control.Enabled = enable;
                }
            }
        }

        public static async Task SetStatus(int timeDelay, string message, RichTextBox rtxtStatus)
        {
            rtxtStatus.Text = message;
            await Task.Delay(timeDelay);
        }
    }
}
